/**
 * 新增社区专用模型
 */

/**
 * 01 新增社区
 */
var lean = require('../../../base/lean.js')
exports.newCommunity = function(firstStep,secondStep,thirdStep,callback) {
    var property = firstStep.property
    var community = firstStep.community
    var communityName =  firstStep.community.name
    var levelNum = secondStep.level
    var blockLevel = secondStep.blockLevel
    var eachLevelName = secondStep.eachLevel
    var buildingData = secondStep.buildingData
    var deviceList = thirdStep

    addProperty(property,function(data3){
        var propertyId = data3
        newPropertyAccount(propertyId,property,function(data4){
            if(data4 == 1){
                newCommunity(propertyId,community,levelNum,function(data7){
                    if(data7 != 2){
                        var communityId = data7
                        // 添加房屋
                        newBuild(communityId,buildingData,eachLevelName,levelNum,blockLevel,function(data14){
                            callback(1)
                        })
                        // 添加设备
                        addDevice(communityId,deviceList,function(data18){
                            console.log(data18)
                        })
                    }else{
                        callback(2)  // 社区已存在
                    }
                })
            }else{
                callback(0) // 添加失败
            }
        })
    })


    // 添加物业
    function addProperty(property,callback) {
        var cql1 = "select * from Property where p_name = '" + property.name + "'"
        lean.cql(cql1,function(data){
            var res1 = data.results
            if(res1 == ''){
                var cql2 = "insert into Property (p_name,p_province,p_city,p_area,p_address,p_head,p_phone) VALUES ('"
                            + property.name + "','"
                            + property.region.province
                            + "','" + property.region.city
                            + "','" + property.region.district
                            + "','" + property.address
                            + "','" + property.head
                            + "','" + property.phone + "')"
                lean.cql(cql2,function(data2){
                    console.log(data2)
                    var res2 = data2.results[0]
                    callback(res2.id)
                })
            }else{
                callback(res1[0].id)
            }
        })
    }

    // 添加物业管理
    function newPropertyAccount(propertyId,property,callback) {
        var cql3 = "select * from PropertyMan where pm_phone = '" + property.account + "'"
        lean.cql(cql3,function(data3){
            if(data3.results == ''){
                var cql4 = "insert into PropertyMan (pm_phone,pm_pwd,pm_p,pm_name) VALUES ('" + property.account + "','e10adc3949ba59abbe56e057f20f883e',pointer('Property','" + propertyId + "'),'" + property.name + "')"
                lean.cql(cql4,function(data4){
                    console.log(data4)
                    callback(1)
                })
            }else{
                callback(1)
            }
            callback(0)
        })
    }

    // 添加社区
    function newCommunity(propertyId,community,levelNum,callback) {
        var cql5 = "select * from Community where c_name = '" + community.name + "'"
        lean.cql(cql5,function(data5){
            console.log('data5' + data5)
            // if(data5.results != ''){
            //     callback(2) // 社区已存在
            // }else{
                var cql6 = "insert into Community (c_p,c_name,c_province,c_city,c_area,c_treeLevel,c_address) VALUES (pointer('Property','"
                + propertyId + "'),'" + community.name + "','" + community.region.province + "','" + community.region.city + "','" + community.region.district + "'," + levelNum + ",'" + community.address + "')"
                lean.cql(cql6,function(data6){
                    console.log('data6' + data6)
                    callback(data6.results[0].id)
                })
            // }
        })
    }


    // 新增一个房屋
    function newBuild(communityId,buildingData,eachLevelName,levelNum,blockLevel,callback) {
        var tree = []
        var tempBuild = []
        for(var i=0;i<levelNum;i++){
            tempBuild[i] = {}
        }
        for(var a=0;a<buildingData.length;a++){
            var y = a
            var building = buildingData[a].房屋编号.split(/-/)
            console.log('building' + building)
            blockName = ""
            room = ""
            for(var b=0;b<building.length;b++){
                var x = b
                var id = building[b]
                console.log('临时临时')
                console.log(tempBuild)
                if(b == 0){
                    console.log(building[b])
                    if(tempBuild[b][id] == undefined){
                        var child = []
                        tempBuild[b][id] = {
                            "b_buildingId": building[b],
                            "b_name" : building[b],
                            "b_id": building[b],
                            "b_level": b+1,
                            "b_type": eachLevelName[b+1],
                            "child": child
                        }
                        tree.push(tempBuild[b][id])
                    }
                }else{
                        if(tempBuild[b][id] == undefined){
                            console.log('其他级数')
                            var child = []
                            tempBuild[b][id] = {
                                "b_buildingId": id,
                                "b_name" : id,
                                "b_id": id,
                                "b_level": b+1,
                                "b_type": eachLevelName[b+1],
                                "child": child
                            }
                            if(blockLevel == b+1){
                                // 当前是栋
                                blockName = building[b]
                            }

                            if(b == building.length-1){
                                if(blockName == ""){
                                    blockName = building[blockLevel -1]
                                }
                                // 当前是室
                                console.log('当前是室')
                                room =  building[b]
                                console.log("======blockLevel" + blockName)
                                var cqlRoom = "insert into House (h_name,h_block,h_blockLevel,h_level,h_c) VALUES ('" + room + "','" + blockName + "'," + blockLevel + "," + levelNum +",pointer('Community','" + communityId + "'))"
                                lean.cql(cqlRoom,function(data10){
                                    console.log('data10',data10)
                                    buildingId = data10.results[0].id
                                    console.log(buildingId)
                                    tempBuild[x][id].b_buildingId = buildingId
                                    tempBuild[x][id].b_id = buildingId
                                    console.log(building[x])
                                    newMan(buildingData[y],function(data15){
                                        newJM(buildingId,data15,communityId,communityName,function(data17){
                                            console.log(data17)
                                        })
                                    })
                                    newCard(buildingId,buildingData[y],communityId,function(data16){
                                        console.log(data16)
                                    })

                                })
                            }
                            console.log(b-1)
                            var parentId = building[b-1]
                            console.log('parentId' + parentId )
                            var parent = tempBuild[b-1][parentId]
                            console.log(parent)
                            console.log(tempBuild[b])
                            parent.child.push(tempBuild[b][id])
                        }
                    }
            }

            }
        }

    function newBuild(){

    }

    // 判断一个人手机是否重复
    function isHaveDouble(phoneList,callback) {

    }

    // 新增一个人
    function newMan(build,callback) {
        console.log(build)
        name = build.户主姓名
        sexText = build.户主性别
        console.log('户主性别 ==============')
        phone = build.户主手机
        sex = false
        if(sexText == "男"){
            sex = true
        }
        var cql11 = "insert into JustMan (jm_sex,jm_phone,jm_realname) VALUES (" + sex + ",'" + phone + "','" + name + "')"
        lean.cql(cql11,function(data11){
            callback(data11.results[0].id)
        })
    }

    // 在房屋下加人
    function newJM(buildingId,manId,communityId,communityName,callback) {
        var cql12 = "insert into b_jm (justMan,building,b_jm_communityName,b_jm_type,b_jm_c) VALUES (pointer('JustMan','"
        + manId + "'),pointer('House','" + buildingId + "'),'" + communityName + "',1,pointer('Community','" + communityId + "'))"
        lean.cql(cql12,function(data12){
            callback(1)
        })
    }

    // 判断同一小区卡号是否重复
    function isCardDouble(cardList,callback) {

    }

    // 新增一个卡
    function newCard(buildingId,build,communityId,callback) {
        console.log(build)
        var cql13 = "insert into BuildingCard (bc_c,bc_b,bc_id) VALUES (pointer('Community','" + communityId + "'),pointer('House','" + buildingId + "'),'" + build.卡片 + "')"
        lean.cql(cql13,function(data13){
            callback(1)
        })

    }

    // 加设备
    function addDevice(communityId,deviceList,callback) {
        if(deviceList != ""){
            var position = ""
            for(var e=0;e<deviceList.length;e++){
                if(position == ""){
                    position = "('" + deviceList[e] + "',pointer('Community','" + communityId + "'))"
                }else{
                    position = position + ",('" + deviceList[e] + "',pointer('Community','" + communityId + "'))"
                }
            }
            console.log(position)
            if(position != ""){
                var cql14 = "insert into Device (d_position,d_c) VALUES " + position
                lean.cql(cql14,function(data){
                    var results = data.results;
                    callback(results);
                });
            }

        }

    }

}
